
#include <stdlib.h>	
#include <stdio.h>
#include <unistd.h>	/*fork()*/
#include <sys/types.h>	/*getpid()*/
#include <sys/wait.h>	/*wait(&status)*/

/*fork -> duplica o processo atual(clona o programa). o clonado torna-se filho*/
/*while [ true ]; do clear; ps -uf; sleep 1; done*/
int main(int argc, char *argv[]){
/*while(1){ LEIA_LINHA_DE_COMANDO; PROCESSO_LINHA_DE_COMANDO; }*/

	int pid;
	int count;
	int status;
	
	printf("Hello world: I'm your father, PID: %d\n", getpid());
	count = 10;

	pid = fork(); /*cria um clone*/
/*Pai -> PID do filho
  No filho -> PID = 0*/
	
	if(pid < 0){					/*ERRO*/
		printf("fork failed\n");
		exit(EXIT_FAILURE);
	}
	
	else if(pid == 0){	/*FILHO*/
		printf("nooooooo. My PID %d\n", getpid());
		printf("My father's PID is %d\n", getppid());
		while(count){
			printf("Luke: %d\n", count--);
			sleep(2);
		}
		printf("Luke, done\n");
	
	}
	
	else if(pid > 0){			/*PAI*/
		printf("Luke, I'm your father. My PID %d\n", getpid());	
		while(count){
			printf("Vader: %d\n", count--);
			sleep(1);
		}
		printf("Vader, done\n");
		printf("I 'll be waiting for you Luke...\n");
		wait(&status); /*espera o filho terminar*/
		printf("Vader: Child returned %d\n", WEXITSTATUS(status)); /*o valor retornado eh o da main do flho*/
	}

	return EXIT_SUCCESS; /*macro q funciona em qqr SO*/
}
